//
//  UIScrollView+Extension.swift
//  TuneMate
//
//  Created by 丸子 on 2023/11/23.
//

import UIKit
import MJRefresh

extension UIScrollView {
    // 添加下拉刷新功能
    func addHeaderRefresh(_ callback: @escaping () -> Void) {
        let header = MJRefreshNormalHeader(refreshingBlock: callback)
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.textColor = Theme.color.text_d1
        header.loadingView?.color = Theme.color.text_d1
        header.setTitle("加载中...".localized(), for: .refreshing)
        header.setTitle("", for: .noMoreData)
        header.setTitle("", for: .willRefresh)
        header.setTitle("", for: .idle)
        header.setTitle("", for: .pulling)
        resetNoMoreData()
        self.mj_header = header
    }
    
    // 添加上拉加载更多功能
    func addFooterRefresh(_ callback: @escaping () -> Void) {
        let footer = MJRefreshAutoNormalFooter(refreshingBlock: callback)
        self.mj_footer = footer
    }
    
    // 开始下拉刷新
    func beginHeaderRefresh() {
        DispatchQueue.main.async {   
            self.mj_header?.beginRefreshing()
        }
    }
    
    // 结束下拉刷新
    func endHeaderRefresh() {
        DispatchQueue.main.async{
            self.mj_header?.endRefreshing()
        }
    }
    
    // 结束上拉加载更多
    func endFooterRefresh() {
        DispatchQueue.main.async {
            self.mj_footer?.endRefreshing()
        }
    }
    
    // 显示没有更多数据并禁止上拉加载
    func showNoMoreData(isNoMoreData:Bool) {
        DispatchQueue.main.async {
            if isNoMoreData {
                self.mj_footer?.endRefreshingWithNoMoreData()
            }else {
                self.mj_footer?.endRefreshing()
            }
        }
    }
    
    // 重置没有更多数据的状态
    func resetNoMoreData() {
        DispatchQueue.main.async {
            self.mj_footer?.resetNoMoreData()
        }
    }
}
#if canImport(UIKit)
import UIKit
import CoreGraphics

public extension UIFont {

    // MARK: - PingFangSC
    static func pingFangSCRegular(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Regular", size: ofSize) ?? UIFont.systemFont(ofSize: ofSize)
    }

    static func pingFangSCUltralight(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Ultralight", size: ofSize) ?? UIFont.pingFangSCRegular(ofSize: ofSize)
    }

    static func pingFangSCLight(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Light", size: ofSize) ?? UIFont.pingFangSCUltralight(ofSize: ofSize)
    }

    static func pingFangSCThin(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Thin", size: ofSize) ?? UIFont.pingFangSCLight(ofSize: ofSize)
    }

    static func pingFangSCMedium(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Medium", size: ofSize) ?? UIFont.pingFangSCThin(ofSize: ofSize)
    }

    static func pingFangSCSemibold(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Semibold", size: ofSize) ?? UIFont.pingFangSCMedium(ofSize: ofSize)
    }

    static func pingFangSCBold(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-Bold", size: ofSize) ?? UIFont.pingFangSCSemibold(ofSize: ofSize)
    }

    static func pingFangSCBoldItalic(ofSize: CGFloat) -> UIFont {
        return UIFont.init(name: "PingFangSC-BoldItalic", size: ofSize) ?? UIFont.pingFangSCBold(ofSize: ofSize)
    }
}
#endif
